package m.jclecai.com.adapter;


import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import m.jclecai.com.R;

import java.util.List;

import m.jclecai.com.adapter.base.BaseExpandableListViewAdapter;
import m.jclecai.com.adapter.holder.GroupBuyChildHolder;
import m.jclecai.com.adapter.holder.GroupBuyInfoHolder;
import m.jclecai.com.adapter.holder.ViewHolder;
import m.jclecai.com.listener.OnItemClickListener;
import m.jclecai.com.view.CustomExpandListview;

/**
 * Created by zd on 2017/5/3.
 */

public class ExpandGroupBuyInfoAdapter extends BaseExpandableListViewAdapter<ViewHolder> implements CustomExpandListview.HeaderAdapter{

    Context mContext;
    CustomExpandListview listview;
    List<List> datas;
    private boolean isShowMore;
    OnItemClickListener changed;
    String fllowPeople = "0";
    String teambuy_isshow = "0";//0-完全公开 1-截止后公开 2-仅跟单人可看 3-完全保密
    String type;//场次类型
    String[] lottery;//当任9场时 投注方案在lottery
    String title = "",term = "";//列表标题-追号期数
    boolean showBuyRecord = false;//默认不显示购买记录
    String orderMoney = "";//订单金额

    public ExpandGroupBuyInfoAdapter(Context context, CustomExpandListview listview,List<List> datas,
                                     OnItemClickListener changed) {
        this.changed = changed;
        this.mContext = context;
        this.listview = listview;
        this.datas = datas;
    }

    public void setLottery(String ly){
        if(!TextUtils.isEmpty(ly))
            lottery = ly.split(",");
    }

    public void setOrderMoney(String orderMoney) {
        this.orderMoney = orderMoney;
    }

    public void setType(String type){
        this.type = type;
    }

    public void setFllowPeople(String fllowPeople){
        this.fllowPeople = fllowPeople;
    }

    public void setTerm(String term){
        this.term = term;
    }

    @Override
    public ViewHolder onCreateChildViewHolder(int groupPosition, int childPosition, boolean isLastChild,
                                              View convertView, ViewGroup parent) {
        return new GroupBuyChildHolder(this.mContext, R.layout.item_group_buy_detail);
    }

    @Override
    public void onBindChildViewHolder(int groupPosition, int childPosition,
                                      boolean isLastChild, ViewHolder viewHolder) {
        List data = datas.get(groupPosition);
        GroupBuyChildHolder holder = (GroupBuyChildHolder)viewHolder;
        holder.init(data,groupPosition,isShowMore,changed,type,lottery,title,showBuyRecord,orderMoney);
    }

    @Override
    public ViewHolder onCreateParentViewHolder(int groupPosition, boolean isExpanded,
                                               View convertView, ViewGroup parent) {
        return new GroupBuyInfoHolder(this.mContext, R.layout.item_group_buy_parent);
    }

    @Override
    public void onBindParentViewHolder(int groupPosition, boolean isExpanded, ViewHolder viewHolder) {
        GroupBuyInfoHolder vh = (GroupBuyInfoHolder) viewHolder;
        vh.init(groupPosition,teambuy_isshow,fllowPeople,isExpanded,term);
    }

    @Override
    public int getGroupCount() {
        return datas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return groupPosition == -1 ? -1 : 1;
    }

    @Override
    public int getHeaderState(int groupPosition, int childPosition) {
        final int childCount = getChildrenCount(groupPosition);
        if (childPosition == childCount - 1) {
            return PINNED_HEADER_PUSHED_UP;
        } else if (childPosition == -1
                && !listview.isGroupExpanded(groupPosition)) {
            return PINNED_HEADER_GONE;
        } else {
            return PINNED_HEADER_VISIBLE;
        }
    }

    @Override
    public void configureHeader(View header, int groupPosition, int childPosition, int alpha) {
        if (groupPosition > -1) {
            TextView title = (TextView)header.findViewById(R.id.list_item_parent_title);
            TextView tv_tag = (TextView)header.findViewById(R.id.tv_tag);
            if(groupPosition == 0) {
                title.setText("投注信息");
                tv_tag.setText(teambuy_isshow);
                tv_tag.setTextColor(ContextCompat.getColor(mContext,R.color.login_txt));
            }else {
                if(!TextUtils.isEmpty(term)){
                    String str[] = term.split(",");
                    title.setText(str[0]);
                    tv_tag.setText(str[1]);
                }else {
                    title.setText("跟单人数");
                    tv_tag.setText(fllowPeople);
                }
                tv_tag.setTextColor(ContextCompat.getColor(mContext,R.color.a));
            }
        }
    }

    public void setTitle(String title){
        this.title = title;
    }

    //显示加载更多
    public void isShowMore(boolean isShowMore){
        this.isShowMore = isShowMore;
    }

    public void setTeambuy_isshow(String teambuy_isshow){
        this.teambuy_isshow = teambuy_isshow;
    }

    public void setShowBuyRecord(boolean showBuyRecord) {
        this.showBuyRecord = showBuyRecord;
    }
}
